package com.mega.ycy.common.item;

import com.mega.ycy.ColorPutter;
import com.mega.ycy.Yiciyuan_EVENT;
import com.mega.ycy.common.entity.EntityDelete;
import com.mega.ycy.common.specialattack.Delete;
import com.mega.ycy.mixin.TWEAKER;
import com.mega.ycy.network.TimestopMessage;
import com.mega.ycy.network.NetworkHandler;
import mods.flammpfeil.slashblade.ItemSlashBladeNamed;
import mods.flammpfeil.slashblade.SlashBlade;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Enchantments;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.opengl.GL11;

import javax.annotation.Nullable;
import java.util.*;

public class ItemAYiSlashBlade extends ItemSlashBladeNamed {
    public static List<String> ycy = new ArrayList<>();
    public boolean isUsing = false;
    private boolean firstload = false;
    public static List<UUID> ycy_death = new ArrayList<>();
    public static List<Entity> ycy_death2 = new ArrayList<>();
    public boolean usedBefore = false;

    public ItemAYiSlashBlade() {
        super(ToolMaterial.IRON, 4.0F);
        this.setMaxDamage(-1);
        this.setUnlocalizedName("flammpfeil.slashblade.named");
        this.setCreativeTab(CreativeTabs.COMBAT);
    }

    public static void create(EntityPlayer player, Entity target) {
        EntityDelete entityDelete;
        if (Objects.isNull(target)) {
            entityDelete = new EntityDelete(player.world, player, 10000.0F);
            entityDelete.addedToChunk = true;
            entityDelete.setPositionAndUpdate(player.posX + player.getLookVec().x * 30.0D, player.posY + player.getLookVec().y * 30.0D, player.posZ + player.getLookVec().z * 30.0D);
            entityDelete.world.onEntityAdded(entityDelete);
        } else {
            entityDelete = new EntityDelete(player.world, player, 10000.0F);
            entityDelete.addedToChunk = true;
            entityDelete.setPositionAndUpdate(target.posX, target.posY + 1.0D, target.posZ);
            entityDelete.world.onEntityAdded(entityDelete);
        }
    }

    @Nullable
    public CreativeTabs getCreativeTab() {
        return SlashBlade.tab;
    }

    @SideOnly(Side.CLIENT)
    public void addInformation(ItemStack par1ItemStack, World world, List<String> par3List, ITooltipFlag inFlag) {
        super.addInformation(par1ItemStack, world, par3List, inFlag);
        par3List.add(ColorPutter.Set("\u201c\u4f60\u4ee5\u4e3a\u4f60\u6210\u4e3a\u4e86\u6700\u5f3a\u3002\u201d", ColorPutter.P_, 1));
        par3List.add("");
        par3List.add(ColorPutter.Set("\u201c\u6210\u4e3a\u4e86\u4e16\u754c\u4e4b\u5dc5\u7684\u5b58\u5728\u3002\u201d", ColorPutter.P_, 1));
        par3List.add("");
        par3List.add(ColorPutter.Set("\u201c\u4f46\uff0c\u5728\u6211\u770b\u6765\u4f60\u4e0d\u8fc7\u662f\u4e00\u4e32\u6570\u636e\u800c\u5df2\u3002\u201d", ColorPutter.P_, 1));
        par3List.add("");
        par3List.add(ColorPutter.Set("\u201c\u4e0d\u8fc7\uff0c\u6211\u4f1a\u4ec1\u6148\u7684\u7ed9\u4e88\u4f60\u201c\u91cd\u751f\u201d\u7684\u673a\u4f1a\u3002\u201d", ColorPutter.P_, 1));
        par3List.add("");
        par3List.add(ColorPutter.Set("\u4e00\u65e6\u5176\u8d44\u6599\u91cf\u72c2\u66b4\u6d8c\u51fa\uff0c\u90a3\u4fbf\u65e0\u4eba\u5f97\u4ee5\u5e78\u514d\u3002", ColorPutter.P_, 1));
    }

    public void addInformationSpecialAttack(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4) {
        EnumSet<SwordType> swordType = this.getSwordType(par1ItemStack);
        if (swordType.contains(SwordType.Bewitched)) {
            par3List.add(String.format("SA:%s", ColorPutter.rainbow("\u6570\u636e\u526a\u5207")));
        }

    }

    public void onUsingTick(ItemStack stack, EntityLivingBase player, int count) {
        super.onUsingTick(stack, player, count);
        int var6 = this.func_77626_a(stack) - count;
        if (ItemSlashBladeNamed.RequiredChargeTick == var6) {
            this.usedBefore = true;
            Minecraft.getMinecraft().addScheduledTask(() -> {
                Minecraft.getMinecraft().entityRenderer.loadShader(new ResourceLocation("shaders/post/desaturate.json"));
                NetworkHandler.INSTANCE.sendMessageToAll(new TimestopMessage(true));
            });
        }

        if (ItemSlashBladeNamed.RequiredChargeTick <= var6) {
            player.playSound(SoundEvents.ENTITY_ENDERMEN_TELEPORT, 0.3F, 1.0F);
        }

    }

    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft) {
        Minecraft mc = Minecraft.getMinecraft();
        mc.addScheduledTask(() -> mc.entityRenderer.loadEntityShader(mc.player));
        NetworkHandler.INSTANCE.sendMessageToAll(new TimestopMessage(false));
        if (entityLiving instanceof EntityPlayer && this.usedBefore) {
            (new Delete()).doSpacialAttack(stack, (EntityPlayer)entityLiving);
            create((EntityPlayer)entityLiving, Delete.getEntityToWatch((EntityPlayer)entityLiving));
            this.usedBefore = false;
        }
        worldIn.getWorldInfo().setRaining(false);
        worldIn.getWorldInfo().setThundering(false);

    }

    public String getItemStackDisplayName(ItemStack stack) {
        return ColorPutter.toCustomF(I18n.format("item.ycy.name1" ), ColorPutter.C1, 50.0D) + ColorPutter.toCustomF(I18n.format("item.ycy.name2"), ColorPutter.C2, 50.0D);
    }

    public void onUpdate(ItemStack arg0, World arg1, Entity arg2, int arg3, boolean arg4) {
        super.onUpdate(arg0, arg1, arg2, arg3, arg4);
        Yiciyuan_EVENT.SAFE_ITEMSTACKS.put(arg0, true);
        ycy.add(arg2.getUniqueID().toString());
        if (arg2 instanceof EntityPlayer) {
            this.isUsing = false;
        }

        ItemSlashBlade.specialAttacks.put(678, new Delete());
        NBTTagCompound tag = arg0.getTagCompound();
        ItemSlashBladeNamed.CurrentItemName.set(tag, "flammpfeil.slashblade.named.yiciyuan");
        ItemSlashBladeNamed.CustomMaxDamage.set(tag, 32767);
        ItemSlashBladeNamed.IsDefaultBewitched.set(tag, true);
        ItemSlashBlade.setBaseAttackModifier(tag, 100.0F);
        ItemSlashBlade.SpecialAttackType.set(tag, 678);
        ItemSlashBlade.TextureName.set(tag, "named/yiciyuan/texture");
        ItemSlashBlade.ModelName.set(tag, "named/yiciyuan/model");
        ItemSlashBlade.RepairCount.set(tag, 100000);
        ItemSlashBlade.KillCount.set(tag, 1000000);
        ItemSlashBlade.ProudSoul.set(tag, 1000000);
        if (!this.firstload) {
            this.firstload = true;
            arg0.setTagCompound(tag);
        }

        if (EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, arg0) <= 0) {
            arg0.addEnchantment(Enchantments.INFINITY, 100);
            arg0.addEnchantment(Enchantments.SHARPNESS, 100);
            arg0.addEnchantment(Enchantments.FLAME, 100);
            arg0.addEnchantment(Enchantments.POWER, 100);
        }

        Objects.requireNonNull(tag).setBoolean("Unbreakable", true);
    }

    public boolean onDroppedByPlayer(ItemStack item, EntityPlayer player) {
        return super.onDroppedByPlayer(item, player);
    }

    @Override
    public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack stack) {
        TWEAKER.reloadFields();
        return super.onEntitySwing(entityLiving, stack);
    }

    public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) {
        Yiciyuan_EVENT.killEntity(entity);
        Delete.spawnParticle(entity.world, entity);
        return super.onLeftClickEntity(stack, player, entity);
    }
}
